package com.app.worldcupapp.list;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.worldcupapp.R;
import com.app.worldcupapp.adapter.MyListAdapter;
import com.app.worldcupapp.adapter.MyTeamsOrderListAdapter;
import com.app.worldcupapp.bean.TeamsBean;
import com.app.worldcupapp.bean.TeamsOrderBean;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TeamBoardActivity extends AppCompatActivity {
TextView rankId,stateId,integral;
ImageView orderTeamId;
String TAG = "TeamBoardActivity";
ListView listView;
ImageButton back;
TabLayout tab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_board);
        initView();
        getData();
        back();

        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 1:
                        Intent intent = new Intent(TeamBoardActivity.this,TeamsListActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void back() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url("http://192.168.27.206:8080/app20/teamboard/list")
                .post(body)
                .build();

        final Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                TeamsOrderBean teamsOrderBean = gson.fromJson(data, TeamsOrderBean.class);
                if (teamsOrderBean.isSuccess()) {
                    // 获取链表数据
                    List list = teamsOrderBean.getList();
                    //使用runOnUITHread才能获取到
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 构建适配器对象
                            MyTeamsOrderListAdapter adapter = new MyTeamsOrderListAdapter(list, getApplicationContext());
                            // 为listview设置适配器对象
                            listView.setAdapter(adapter);
                        }
                    });


                } else {
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        tab = findViewById(R.id.orderTabId);
        rankId = findViewById(R.id.rankId);
        stateId = findViewById(R.id.stateId);
        integral = findViewById(R.id.integral);
        orderTeamId = findViewById(R.id.orderTeamId);
        back = findViewById(R.id.orderBackId);
        listView = findViewById(R.id.teamsBoardId);
    }
}